<template>
  <div>
    <vol-box class="accountin" :model.sync="model" title="账务处理" :width="1100" :height="550">
      <VolHeader icon="md-apps" text="">
        <div style="text-align: right; padding-bottom: 4px">
          <Button size="small" type="info" icon="logo-freebsd-devil" @click="getForm">充值</Button>
          <Button size="small" type="info" icon="logo-freebsd-devil"
            @click="consumptionForm">消费</Button>
          <Button size="small" type="info" icon="logo-yen" @click="transferAccountsForm">转账</Button>
          <!-- <Button type="info" icon="logo-yen" size="small" @click="refundForm"
            >退款</Button
          > -->
          <Button size="small" type="info" icon="ios-create" @click="adjustmentForm">调整</Button>
          <Button size="small" type="info" icon="ios-albums" @click="reverseEntryForm">冲账</Button>
          <Button size="small" type="info" icon="ios-albums" @click="reSetAccount"
            v-show="this.reSetAccountParam" v-loading="restLoad">重置</Button>
          <Button size="small" type="info" icon="ios-checkmark-circle"
            @click="modelExamine = true">审核</Button>
        </div>
      </VolHeader>
      <VolForm ref="myform" :loadKey="loadKey" :formFileds="formFileds" :formRules="formRules">
      </VolForm>
      <vtable ref="table" @getRows="getRows" :isSingle="true" style="margin-bottom: 20px"></vtable>
      <div slot="footer">
        <Button type="info" size="small" icon="md-close" @click="model = false">关闭</Button>
      </div>
      <!-- 充值  -->
      <Income ref="Income" @refreshTables="refreshTables"></Income>
      <!-- 消费  -->
      <Consumption ref="consumption" @refreshTables="refreshTables"></Consumption>
      <!-- 审核弹框 -->
      <!-- <ToExamine ref="toExamine"></ToExamine> -->
      <!-- 冲账弹框 -->
      <ReverseEntry ref="reverseEntry" @refreshTables="refreshTables"></ReverseEntry>
      <!-- 调整弹框 -->
      <Adjustment ref="adjustment" @refreshTables="refreshTables"></Adjustment>
      <!-- 转账弹框 -->
      <TransferAccounts ref="transferAccounts" @refreshTables="refreshTables">
      </TransferAccounts>
    </vol-box>
    <vol-box :model.sync="modelExamine" title="审核" :width="300" :height="220">
      <VolForm ref="myform" :loadKey="loadKey" :formFileds="formFileds01" :formRules="formRules01"
        :labelWidth="100"></VolForm>
      <!-- 添加slot槽覆盖footer的按钮，添加新的按钮 -->
      <div slot="footer">
        <Button size="small" type="success" @click="examineForm">审核</Button>
        <Button size="small" type="info" @click="modelExamine = false">关闭</Button>
      </div>
    </vol-box>
  </div>
</template>
<script>
import bus from "@/utilities/bus.js";

import VolForm from "@/components/basic/VolForm.vue";
import VolHeader from "@/components/basic/VolHeader.vue";
import vtable from "./accouting";
import Income from "./Income.vue"; //入账
// import ToExamine from "./toExamine.vue"; //审核
import ReverseEntry from "./reverseEntry.vue"; //冲账
import Adjustment from "./adjustment.vue"; //调整
import TransferAccounts from "./transferAccounts.vue"; //转账

export default {
  components: {
    VolBox: () => import("@/components/basic/VolBox.vue"),
    VolForm,
    VolHeader,
    vtable,
    Income,
    Consumption: () => import("./consumption.vue"), //消费
    // ToExamine,
    ReverseEntry,
    Adjustment,
    TransferAccounts,
  },
  created() {
    bus.$on("refresh", (msg) => {
      this.refreshTable();
    });
  },
  data() {
    return {
      // 加载
      reSetAccountParam: false,
      loading: true,
      loadKey: true,
      row: {},
      rows: [], //选中表格数据
      // url: "api/App_Expert/getPageData", //后台加载数据的url
      model: false,
      modelExamine: false,
      // pagination: {
      //   total: 0, //分页总数量
      //   size: 30, //分页大小,30,60,100.
      //   sortName: "CreateDate", //从后加载数据分页时的排序字段
      // },
      formFileds: {
        account_code_fk: "", //应收帐户编号（后台生成）
        account_alias: "", //应收账户编号
        account_name: "", //应收帐户名称
        contact: "", //联系人
        contact_phone: "", //联系电话
        temporary_limit: "", //临时限额
        effec_startdate: "", //开始时间
        effec_enddate: "", //结束时间
        account_limit: "", //单笔挂账限额
        warn_limit: "", //警告限额
        warn_phone: "", //警告手机号
        finance_notes: "", //财务备注（表格）
        notes: "", //备注
        is_valid: "", //是否有效
        balance_credit: null, //授信余额
        balance_recharge: null, //充值余额
        balance_total: null, //总余额
      },
      formRules: [
        //两列的表单，formRules数据格式为:[[{},{}]]
        [
          {
            title: "应收账户名称",
            field: "account_name",
            type: "text",
            disabled: true,
            colSize: 4,
          },
          {
            title: "应收账户编号",
            field: "account_alias",
            type: "text",
            disabled: true,
            colSize: 4,
          },
          {
            title: "联系人",
            field: "contact",
            type: "text",
            disabled: true,
            colSize: 2,
          },
          {
            title: "联系人电话",
            field: "contact_phone",
            type: "text",
            disabled: true,
            colSize: 2,
          },
        ],
        [
          {
            title: "单笔挂账限额",
            field: "account_limit",
            type: "text",
            disabled: true,
          },
          {
            title: "授信余额",
            field: "balance_credit",
            type: "text",
            disabled: true,
          },
          {
            title: "充值余额",
            field: "balance_recharge",
            type: "text",
            disabled: true,
          },
          {
            title: "可用余额",
            field: "balance_total",
            type: "text",
            disabled: true,
          },
        ],
        [
          {
            title: "临时限额",
            field: "temporary_limit",
            type: "text",
            disabled: true,
            colSize: 3,
          },
          {
            title: "备注",
            field: "notes",
            type: "text",
            disabled: true,
            colSize: 9,
          },
        ],
      ],
      formFileds01: { approvedRemark: "", ZT: "通过" },
      formRules01: [
        [
          {
            title: "状态",
            field: "ZT",
            type: "text",
            disabled: true,
            colSize: 12,
          },
        ],
        [
          {
            title: "备注",
            field: "approvedRemark",
            type: "text",
            colSize: 12,
          },
        ],
      ],
      restLoad: false,
    };
  },
  methods: {
    open(row) {
      console.log("行", row);
      let permission = this.$store.state.system.permission;
      permission.forEach((item) => {
        if (item.name == "应收账户") {
          item.permission.forEach((itemChild) => {
            if (itemChild == "ResetAccode") {
              this.reSetAccountParam = true;
            }
          });
          console.log("应收账户", item);
        }
      });

      this.$store.getters.data().viewPageTest = this;
      this.row = row;
      this.model = true;
      this.formFileds.account_name = row.account_name; //应收帐户名称
      this.formFileds.contact = row.contact;
      this.formFileds.contact_phone = row.contact_phone;
      this.formFileds.temporary_limit = row.temporary_limit;
      this.formFileds.effec_startdate = row.effec_startdate;
      this.formFileds.effec_enddate = row.effec_enddate;
      this.formFileds.account_limit = row.account_limit;
      this.formFileds.warn_limit = row.warn_limit;
      this.formFileds.warn_phone = row.warn_phone;
      this.formFileds.notes = row.notes; //备注
      this.formFileds.is_valid = row.is_valid;
      this.formFileds.account_alias = row.account_alias; //应收帐户编号
      this.formFileds.balance_credit = row.balance_credit;
      this.formFileds.balance_recharge = row.balance_recharge;
      this.formFileds.balance_total = row.balance_total;
      this.formFileds.account_code_fk = row.account_code_fk; //应收账户编号（后台生成）

      this.$refs.table.params.acccode = row.account_code_fk;
      this.$refs.table.open();
    },

    // 刷新表格
    refreshTable() {
      console.log("shuaxin");
      this.$refs.table.params.acccode = this.formFileds.account_code_fk;
      this.$refs.table.AccountsGet();
      this.$refs.Income.model = false;
    },
    // 刷新最外层表格
    refreshTables() {
      console.log("shuaxin2");
      this.$emit("AccountsGet");
    },
    // 充值按钮
    getForm() {
      if (this.formFileds.is_valid) {
        this.$refs.Income.resetForm();
        this.$refs.Income.model = true;
        this.$refs.Income.formFileds.account_code =
          this.formFileds.account_code_fk; //应收账户编号（后端生成）
        this.$refs.Income.formFileds.account_alias =
          this.formFileds.account_alias; //应收账户编号
        this.$refs.Income.formFileds.notesb = this.formFileds.notes; //备注（表格）
        this.$refs.Income.formFileds.account_name =
          this.formFileds.account_name; //应收帐户名称
        this.$refs.Income.formFileds.is_valid = this.formFileds.is_valid; //是否有效
      } else {
        this.$message.error("无效账户禁止充值!");
      }
    },
    // 消费按钮
    consumptionForm() {
      if (this.formFileds.is_valid) {
        this.$refs.consumption.resetForm();
        this.$refs.consumption.model = true;
        this.$refs.consumption.formFileds.account_code =
          this.formFileds.account_code_fk; //应收账户编号（后端生成）
        this.$refs.consumption.formFileds.account_alias =
          this.formFileds.account_alias; //应收账户编号
        this.$refs.consumption.formFileds.notesb = this.formFileds.notes; //备注（表格）
        this.$refs.consumption.formFileds.account_name =
          this.formFileds.account_name; //应收帐户名称
        this.$refs.consumption.formFileds.is_valid = this.formFileds.is_valid; //是否有效
      } else {
        this.$message.error("无效账户禁止消费!");
      }
    },
    // 转账弹框
    transferAccountsForm() {
      this.rows = [];
      console.log("sss", this.rows);
      this.$refs.table.getRows();
      if (this.rows.length > 0) {
        if (this.rows[0].type_func) {
          let typeFunc = this.rows[0].type_func;
          let type = this.rows[0].operate_state;

          if ((typeFunc == 11 || typeFunc == 12) && type == 0) {
            this.$refs.transferAccounts.resetForm();
            this.$refs.transferAccounts.onInit();
            this.$refs.transferAccounts.transferNumber();
            this.$refs.transferAccounts.formFileds.account_alias =
              this.formFileds.account_alias; //应收账户编号
            this.$refs.transferAccounts.formFileds.paccount_name =
              this.formFileds.account_name; //应收帐户名称
            this.$refs.transferAccounts.transferData(this.rows[0]);
            this.$refs.transferAccounts.model = true;
          } else {
            return this.$message.error("该订单不允许转账!");
          }
        } else {
          return this.$message.error("此订单没有业务类型!");
        }
      }
    },
    // 调整弹框
    adjustmentForm() {
      this.rows = [];
      this.$refs.table.getRows();
      if (this.rows.length > 0) {
        if (this.rows[0].type_func) {
          let typeFunc = this.rows[0].type_func;
          let type = this.rows[0].operate_state;

          if (typeFunc == 11 || typeFunc == 12) {
            this.$refs.adjustment.resetForm();
            this.$refs.adjustment.model = true;
            this.$refs.adjustment.formFileds.account_alias =
              this.formFileds.account_alias; //应收账户编号
            // console.log("sss", this.formFileds);
            this.$refs.adjustment.formFileds.account_name =
              this.formFileds.account_name; //应收帐户名称
          } else {
            return this.$message.error("该订单不允许调整!");
          }
        } else {
          return this.$message.error("此订单没有业务类型!");
        }

        this.$refs.adjustment.transferData(this.rows[0]);
      }
    },
    // 冲账按钮
    reverseEntryForm() {
      const newDate = new Date();
      const data = {
        yy: newDate.getFullYear(),
        mm: newDate.getMonth() + 1,
        dd: newDate.getDate(),
      };
      const newMM = data.mm > 10 ? data.mm : "0" + data.mm;
      const newDD = data.dd > 10 ? data.dd : "0" + data.dd;
      const time = data.yy + "-" + newMM + "-" + newDD;
      this.rows = [];
      this.$refs.table.getRows();
      if (this.rows.length > 0) {
        console.log("aa", this.rows[0].createDate);
        if (this.rows[0].createDate) {
          let createDate = this.rows[0].createDate;
          if (createDate.includes(time)) {
            if (this.rows[0].type_func) {
              let typeFunc = this.rows[0].type_func;
              let type = this.rows[0].operate_state;

              if ((typeFunc == 11 || typeFunc == 12) && type == 0) {
                // 判断时删掉
                this.$refs.reverseEntry.resetForm();
                this.$refs.reverseEntry.model = true;
                this.$refs.reverseEntry.transferData(this.rows[0]);
                this.$refs.reverseEntry.formFileds.account_name =
                  this.formFileds.account_name; //应收帐户名称(表格)
                this.$refs.reverseEntry.formFileds.account_alias =
                  this.formFileds.account_alias; //应收帐户编号
              } else {
                return this.$message.error("该订单不允许冲账!");
              }
            } else {
              return this.$message.error("此订单没有业务类型!");
            }
          } else {
            this.$message.error("冲账只限于当日！");
          }
        } else {
          this.$message.error("选中数据没有入账日期！");
        }
      }
    },
    // 重置按钮
    reSetAccount() {
      this.$confirm("重置应收账户余额, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        customClass: "del-model",
        type: "warning",
      }).then(() => {
        this.restLoad = true;
        this.http
          .post("api/AccountsDetail/ReSetAccount", {
            code: this.formFileds.account_code_fk,
          })
          .then((res) => {
            this.restLoad = false;
            if (res.status) {
              this.$Message.success(res.message);
            } else {
              this.$Message.error(res.message);
            }
          });
      });
    },
    // // 审核弹框按钮

    examineForm() {
      this.rows = [];
      this.$refs.table.getRows();
      // this.$confirm("审核应收账户充值余额, 是否继续?", "提示", {
      //   confirmButtonText: "确定",
      //   cancelButtonText: "取消",
      //   customClass: "del-model",
      //   type: "warning"
      // }).then(() => {
      this.http
        .post(
          `api/AccountsDetail/ModifyTypeRecord`,
          {
            internal_id_fk: `${this.rows[0].internal_id_fk}`,
            approvedRemark: this.formFileds01.approvedRemark,
          },
          true
        )
        .then((res) => {
          if (res == 1) {
            if (this.rows.length > 0) {
              this.rows[0].type_func = 11;
              this.http
                .post(
                  `/api/AccountsDetail/AccountsDetailSet`,
                  this.rows[0],
                  true
                )
                .then((res) => {
                  if (res.status) {
                    this.$Message.success("修改成功");
                    this.modelExamine = false;
                    this.$refs.table.AccountsGet();
                  } else {
                    this.$Message.error("修改失败");
                  }
                });
            }
          }
          // });
        });

      //   this.$refs.table.getRows();
      //   if (this.rows.length > 0) {
      //     // 判断时删掉
      //     this.$refs.toExamine.model = true;
      //     this.$refs.toExamine.formFileds.remarks = "";
      //     this.$refs.toExamine.setRows = this.rows;

      //     // 判断是否审核过 isexamine
      //     // if (this.rows[0].isexamine == 1) {
      //     //   this.$message.error("账目已审核!");
      //     // } else {
      //     //   this.$refs.toExamine.model = true;
      //     //   this.$refs.toExamine.formFileds.remarks = "";
      //     //   this.$refs.toExamine.setRows = this.rows;
      //     // }
      //   }
    },
    // 选中表格数据
    getRows(rows) {
      this.rows = rows;
      console.log("选中数据", rows);
    },
    reset() {},
  },
};
</script>

<style lang="scss">
.accountin {
  .v-header {
    margin: 0 !important;
  }
  .ivu-form-item {
    margin-bottom: 0 !important;
  }
}
</style>
